package com.ruoyi.user.controller.app;

import com.ruoyi.common.core.constant.constant.PathConstant;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.pojo.request.PageSearchParam;
import com.ruoyi.common.core.pojo.result.Pages;
import com.ruoyi.common.core.pojo.vo.BaseVO;
import com.ruoyi.common.core.pojo.vo.MultiVO;
import com.ruoyi.common.core.pojo.vo.PageVO;
import com.ruoyi.common.core.pojo.vo.SingleVO;
import com.ruoyi.common.core.result.Result;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.system.api.model.user.AppUserChum;
import com.ruoyi.user.model.pojo.entity.UserChum;
import com.ruoyi.user.model.pojo.entity.UserChumApply;
import com.ruoyi.user.model.pojo.request.app.AppUserChumApplyRequest;
import com.ruoyi.user.model.pojo.request.app.AppUserChumCheckRequest;
import com.ruoyi.user.model.pojo.request.app.AppUserChumCreateRequest;
import com.ruoyi.user.model.pojo.request.app.AppUserChumSearchRequest;
import com.ruoyi.user.model.pojo.result.app.AppUserChumInfoResult;
import com.ruoyi.user.service.IUserChumService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "App-用户密友关系", description = "App-用户密友关系")
@RestController
@RequestMapping(PathConstant.APP_PATH + "user-chum")
public class AppUserChumController {

    @Autowired
    private IUserChumService iUserChumService;


    @Operation(summary = "内部调用获取id", description = "内部调用获取id")
    @GetMapping("getById/{id}")
    public SingleVO<AppUserChum> getUserById(@PathVariable("id") Long id) {
        AppUserChum userBasicsInfo = iUserChumService.info(id);
        return Result.single(userBasicsInfo);
    }


    @Operation(summary = "内部调用设置用户密友位置", description = "内部调用设置用户密友位置")
    @GetMapping("setChum/{id}/{aimUserId}")
    public BaseVO setChum(@PathVariable("id") Long id, @PathVariable("aimUserId") Long aimUserId) {
        return Result.successOrFail(iUserChumService.setChum(id, aimUserId));
    }


    @Operation(description = "开通密友关系位置", summary = "开通密友关系位置")
    @PostMapping("create")
    public BaseVO create(@RequestBody @Validated AppUserChumCreateRequest request) {
        if (request.getAimUserId().equals(0L)) {
            request.setAimUserId(SecurityContextHolder.getUserId());
        }
        return Result.successOrFail(iUserChumService.openChum(request, request.getAimUserId()));
    }

    @Operation(description = "查看自己/他人剩余的密友位置", summary = "查看自己/他人剩余的密友位置")
    @GetMapping("listByUser")
    public MultiVO<UserChum> listByUser(Long userId) {
        if (CommonUtils.isNull(userId)) {
            userId = SecurityContextHolder.getUserId();
        }
        return Result.multi(iUserChumService.getUserChumList(userId, 0L));
    }


    @Operation(description = "查看自己/他人的密友", summary = "查看自己/他人的密友")
    @GetMapping("listPage")
    public PageVO<AppUserChumInfoResult> listPage(AppUserChumSearchRequest request) {
        if (request.getUserId().equals(0L)) {
            request.setUserId(SecurityContextHolder.getUserId());
        }
        Pages<AppUserChumInfoResult> resultPages = iUserChumService.listPage(request);
        return Result.page(resultPages);
    }

    @Operation(description = "密友详情", summary = "密友详情")
    @GetMapping("info")
    public SingleVO<AppUserChumInfoResult> info(Long id) {
        return Result.single(iUserChumService.infoById(id));
    }

    @Operation(description = "申请密友", summary = "申请密友")
    @PostMapping("apply")
    public BaseVO apply(@RequestBody @Validated AppUserChumApplyRequest request) {
        boolean add = iUserChumService.apply(request, SecurityContextHolder.getUserId());
        return Result.successOrFail(add);
    }

    @Operation(description = "申请密友列表", summary = "申请密友列表")
    @GetMapping("applyListPage")
    public PageVO<UserChumApply> applyListPage(PageSearchParam pageSearchParam) {
        Pages<UserChumApply> pages = iUserChumService.applyListPage(pageSearchParam, SecurityContextHolder.getUserId());
        return Result.page(pages);
    }

    @Operation(description = "审核", summary = "审核")
    @PostMapping("check")
    public BaseVO check(@RequestBody @Validated AppUserChumCheckRequest request) {
        boolean add = iUserChumService.check(request, SecurityContextHolder.getUserId());
        return Result.successOrFail(add);
    }

    @Operation(description = "移除密友", summary = "移除密友")
    @PostMapping("removeChum")
    public BaseVO removeChum(@RequestBody @Validated AppUserChumApplyRequest request) {
        boolean add = iUserChumService.removeChum(request, SecurityContextHolder.getUserId());
        return Result.successOrFail(add);
    }

}
